package cn.zmit.kuxi.activity;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.xdroid.common.utils.PreferenceHelper;
import com.xdroid.common.utils.ToastUtils;
import com.xdroid.request.extension.cache.DiskCacheManager;
import com.xdroid.utils.UIKit;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.cache.CacheKey;
import cn.zmit.kuxi.contants.SPConstants;
import cn.zmit.kuxi.request.CookieManager;

/** * @author @parameter * @return */
public class SetActivity extends BaseActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTitleView("设置", true);
		setContentView(R.layout.activity_set);
		ViewUtils.inject(this);
	}
	@OnClick({R.id.rl_set_next_my,R.id.rl_set_next_help,R.id.rl_set_next_service
		,R.id.rl_set_next_exit,R.id.rl_set_next_update})
	private void OnClick(View v){
		switch (v.getId()) {
		case R.id.rl_set_next_my:
			Bundle bundle = new Bundle();
			bundle.putString("id", 1+"");
//			Intent intent = new Intent(SetActivity.this,HelpCenterActivity.class);
			openActivity(HelpDetailsActivity.class,bundle);
			break;
		case R.id.rl_set_next_help:
			Bundle bundle3 = new Bundle();
			bundle3.putString("id", 3+"");
			openActivity(HelpDetailsActivity.class,bundle3);
			break;
		case R.id.rl_set_next_service:
			Bundle bundle2 = new Bundle();
			bundle2.putString("id", 2+"");
			openActivity(HelpDetailsActivity.class,bundle2);
			break;
		case R.id.rl_set_next_update:
			openActivity(AboutActivity.class);
			break;
		case R.id.rl_set_next_exit://退出
			if (PreferenceHelper.readBoolean(context, SPConstants.SHARED_PREFERENCE_NAME, SPConstants.HAS_LOGGED,
					false)) {
				showProgressDialog("退出登录……");
				new Timer().schedule(new TimerTask() {

					@Override
					public void run() {
						UIKit.runOnMainThreadAsync(new Runnable() {
							
							@Override
							public void run() {
								//清除cookie
								CookieManager.getInstance().clearCookies();
								UserActivity.btn_login.setVisibility(View.VISIBLE);
								UserActivity.rl_info.setVisibility(View.GONE);
								UserActivity.imag_user_pic.setImageResource(R.drawable.ic_launcher);
								DiskCacheManager<HashMap<String, Object>>diskCacheManager=new DiskCacheManager<>(context);
								diskCacheManager.removeData(CacheKey.COOKIE);
								PreferenceHelper.write(context, SPConstants.SHARED_PREFERENCE_NAME, SPConstants.HAS_LOGGED, false);
								PreferenceHelper.write(context, SPConstants.SHARED_PREFERENCE_NAME, SPConstants.USERNAME, "");
								PreferenceHelper.write(context, SPConstants.SHARED_PREFERENCE_NAME, SPConstants.USERPHOTO, "");
								
								dismissProgressDialog();
								openActivity(HomeActivity.class);
								finish();
							}
						});

					}
				}, 800);
			}else {
				ToastUtils.show(context, "您未登陆");
			}
			break;
		default:
			break;
		}
	}
}
